package com.jiantong.lawEnforcing.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.lawEnforcing.impl.dao.CauseActionsDao;
import com.jiantong.lawEnforcing.model.CauseActions;
import com.jiantong.lawEnforcing.service.CauseActionsService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 案由列表
 * Created by mz03 on 2017/8/17.
 */
@Slf4j
@Service
@RpcProvider
public class CauseActionsServiceImpl implements CauseActionsService{
    private final CauseActionsDao causeActionsDao;

    @Autowired
    public CauseActionsServiceImpl(CauseActionsDao causeActionsDao) {
        this.causeActionsDao = causeActionsDao;
    }

    /**
     * id 查找
     *
     * @param id
     * @return CauseActions
     */
    @Override
    public Response<CauseActions> finById(Long id) {
        try {
            CauseActions causeActions = causeActionsDao.findById(id);
            if (causeActions != null) {
                return Response.ok(causeActions);
            } else {
                log.error("causeActions (id={}) is not found", id);
                return Response.fail("causeActions.not.found");
            }
        }catch (Exception e){
            log.error("failed to find causeActions(id={}),cause:{}",id, Throwables.getStackTraceAsString(e));
            return Response.fail("causeActions.find.fail");
        }
    }

    /**
     * 创建
     *
     * @param causeActions 单据对象
     * @return 主键 id
     */
    @Override
    public Response<Long> create(CauseActions causeActions) {
        try {
            causeActionsDao.create(causeActions);
            return Response.ok(causeActions.getId());
        }catch (Exception e) {
            log.error("failed.to.create.causeActions:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("create.causeActions.failed");
        }
    }

    /**
     * 更新
     *
     * @param causeActions 单据对象
     * @return true or false
     */
    @Override
    public Response<Boolean> update(CauseActions causeActions) {
        try{
            return Response.ok(causeActionsDao.update(causeActions));
        }catch (Exception e){
            log.error("failed.to.update.causeActions:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("update.causeActions.failed");
        }
    }

    /**
     * 分页查询
     *
     * @param pageNo
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public Response<Paging<CauseActions>> pagingByStatus(Integer pageNo, Integer pageSize, Integer status) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            Map<String, Object> criteria = Maps.newHashMap();
            if(status != null){
                criteria.put("status",status);
            }
            return Response.ok(causeActionsDao.paging(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to find CauseActions(status={}), cause:{}", status, Throwables.getStackTraceAsString(e));
            return Response.fail("CauseActions.find.fail");
        }
    }
}
